package com.qingxin.park.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.ParkBlock;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.ParkBlockService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;

/**
 * 地块
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/block")
public class ParkBlockAction extends BaseAction {
	
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private ParkBlockService parkBlockService;
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/park/block_list";
	}
	
	/**
	 * 获取列表分页数据
	 * @param parkid
	 * @param blockName
	 * @param isuse
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(@RequestParam(value="leftdata")String parkid,
			 String blockName, String blockType, String isuse, PageParam pageParam, HttpServletRequest request){
		return parkBlockService.list(parkid, blockName, blockType, isuse, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加地块信息
	 * @param model
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String parkid, HttpServletRequest request){
		model.put("parkid", parkid);
		FarmPark park = farmParkService.get(parkid);
		model.put("park", park);
		
		return "trace/park/block_add";
	}
	
	
	/**
	 * 保存地块信息
	 * @param block
	 * @param result
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid ParkBlock block, BindingResult result, String parkid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = parkBlockService.add(block, parkid, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改地块信息
	 * @param model
	 * @param blockid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String blockid){
		ParkBlock block = parkBlockService.get(blockid);
		model.put("block", block);
		
		FarmPark park = farmParkService.get(block.getFarmPark().getParkid());
		model.put("park", park);
		
		return "trace/park/block_edit";
	}
	
	
	/**
	 * 保存修改信息
	 * @param block
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid ParkBlock block, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = parkBlockService.update(block, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看地块详情
	 * @param model
	 * @param blockid
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String blockid){
		ParkBlock block = parkBlockService.get(blockid);
		model.put("block", block);
		
		FarmPark park = farmParkService.get(block.getFarmPark().getParkid());
		model.put("park", park);
		
		return "trace/park/block_view";
	}
	
	
	
	/**
	 * 删除地块信息
	 * @param blockid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] blockid, HttpServletRequest request){
		if(blockid==null || blockid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return parkBlockService.delete(blockid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断地块名称是否存在
	 * @param blockName
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String blockName, String blockid, String parkid){
		boolean existed = parkBlockService.isNameDuplicate(blockName, blockid, parkid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断地块编号是否存在
	 * @param blockCode
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isCodeDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isCodeDuplicate(String blockCode, String blockid, String parkid){
		boolean existed = parkBlockService.isCodeDuplicate(blockCode, blockid, parkid);
		return existed ? "1":"0";
	}
	
	
}
